package com.quanquan.store.repository;

import com.quanquan.store.entity.GroupBuyingTradingRecord;
import com.quanquan.store.entity.ShoppingKillTradingRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface GroupBuyingTradingRecordRepository extends JpaRepository<GroupBuyingTradingRecord,String>, JpaSpecificationExecutor<GroupBuyingTradingRecord> {


    /**
     * 根据秒杀商品id查询记录
     * @param groupBuyingId
     * @return
     */
    @Query(value = "SELECT * FROM oo_group_buying_trading_record WHERE  group_buying_id=?1 and (trading_status = 'paid') ", nativeQuery = true)
    public List<GroupBuyingTradingRecord> findByGroupBuyingId(String groupBuyingId);

    /**
     * 根据秒杀商品id查询记录
     * @param groupBuyingId
     * @return
     */
    @Query(value = "SELECT * FROM oo_group_buying_trading_record WHERE  group_buying_id=?1 and (trading_status = 'paid' or trading_status = 'success') ", nativeQuery = true)
    public List<GroupBuyingTradingRecord> findByGroupBuyingId2(String groupBuyingId);
    /**
     * 查询用户秒杀成功次数
     * @param userId
     * @param groupBuyingId
     * @return
     */
    @Query(value = "SELECT count(*) FROM oo_group_buying_trading_record WHERE trading_status = 'paid' and user_id =?1 and group_buying_id =?2", nativeQuery = true)
    Integer findAllCount(String userId, String groupBuyingId);

    @Query(value = "SELECT * FROM oo_group_buying_trading_record WHERE trading_status = 'unpaid'", nativeQuery = true)
    List<GroupBuyingTradingRecord> findAllUnpaid();
}
